﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessObjects.BO_GetObjects;
using DigiLib;
public partial class Administrator_Controls_Admin_Gallery_AddNew : Admin_Controls_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    //load data
    public void LoadData()
    {
        DataTable dt = BO_portal_articles.Get_Type_Gallery();
        base.bindDDL(DDLDanhMuc, dt, "FULL_CATEGORY_NAME", "CATEGORY_ID");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (CheckFile())
        {
            try
            {
                int category_id = int.Parse(DDLDanhMuc.SelectedValue);
                string thumps = "~/Upload/Thumps/" + fubImage.FileName;
                SaveFileUpload();
                string result = BO_portal_articles.Insert(category_id, 1, string.Empty, string.Empty, thumps, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, txtNoiDungTiengViet.Text, txtNoiDungTiengAnh.Text, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, 0, 0, 0, 0, true, chkIsHot.Checked, 0, 4, DateTime.Now, DateTime.Now, string.Empty, DateTime.Now, string.Empty, DateTime.Now, string.Empty, 1);
                if (result == "0")
                {
                    divMessage.InnerHtml = MessageBox.Error("Hệ Thống", "Xảy ra lỗi khi thêm mới");
                }
                else
                {
                    divMessage.InnerHtml = MessageBox.Success("Hệ Thống", "Thêm mới thành công");
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        else
        {
            divMessage.InnerHtml = MessageBox.Error("Lỗi upload","Ảnh không đúng định dạng hoặc kích thước quá lớn");
        }
    }
    public bool CheckFile()
    {
        int size = fubImage.PostedFile.ContentLength / 1000000;
        if (fubImage.PostedFile.ContentType == "image/jpg"
            || fubImage.PostedFile.ContentType == "image/gif"
            || fubImage.PostedFile.ContentType == "image/png"
            || fubImage.PostedFile.ContentType == "image/jpeg"
            || fubImage.PostedFile.ContentType == "image/bmp" && size < 4)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    //save file upload
    public void SaveFileUpload()
    {
        fubImage.SaveAs(Server.MapPath("~/Upload/Thumps/" + fubImage.FileName));
    }
}